<?php
class SRDO {
	public static $config;
	private static $errorHandler = null;
	private static $debugHandler = null;
	
	public static function generateDynamicClass($table) {
		$def = SRDO_Table::getTable(strtolower($table));

		if ($def) {
			$t = 'class ' . ucwords($def['table']) . " extends SRDO_Object\n";
			$t .= "{\n";
			$t .= "\tvar \$__srdo	= 'DynamicClass';\n";
			$t .= "\tprivate \$db 		= '{$def['database']}';\n";
			$t .= "\tprivate \$table 	= '{$def['table']}';\n";
			$t .= "\tvar \$" . implode(";\n\tvar \$", array_keys($def['field'])) . ";\n";
			$t .= "public function __constructor() { \$this->__constructor(); }\n";
			$t .= "}\n";
	
			return (eval($t) === false ? false : true);
		}

		return false;
	}

	public function type($type, $sql = null, $fk = null) {
		return new SRDO_Type($type, $sql, $fk);
	}
	
	public function validate() {
		foreach ( array_keys( self::$config['table'] ) AS $t ) {
			SRDO_Table::validate( $t );
		}
	}
	
	public function getTableNames() {
		return array_keys( self::$config['table'] );
	}
	
	public static function &factory($table) {
		return SRDO_Object::factory($table);
	}
	
	
	public static function registerLogging($error, $debug = null) {
		self::$errorHandler = $error;
		self::$debugHandler	= $debug;
	}
	
	public function error($message) {
		if ( self::$errorHandler !== null ) {
			call_user_func(self::$errorHandler, $message);
		}
	}
	
	public function debug($level, $message) {
		if ( self::$debugHandler !== null ) {
			call_user_func(self::$debugHandler, $level, $message);
		}
	}
}

require_once( dirname( __FILE__ ) . '/SRDO_SQL.class.php' );
require_once( dirname( __FILE__ ) . '/SRDO_Type.class.php' );
require_once( dirname( __FILE__ ) . '/SRDO_Table.class.php' );
require_once( dirname( __FILE__ ) . '/SRDO_Database.class.php' );
require_once( dirname( __FILE__ ) . '/SRDO_DataObject.class.php' );
require_once( dirname( __FILE__ ) . '/SRDO_DataObjectField.class.php' );
require_once( dirname( __FILE__ ) . '/SRDO_Object.class.php' );